<include file='Common:header'/>

<link rel="stylesheet" href="__PUBLIC__/resource/plugins/zTree/zTreeStyle/zTreeStyle.css">

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            API文档管理
            <small></small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:U('Index/index')}"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li class="active">API文档管理</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">

        <!-- START CUSTOM TABS -->
        <div class="row">
            <div class="col-xs-12">
                <!-- Custom Tabs -->
                <div class="nav-tabs-custom">

                    <ul class="nav nav-tabs">

                        <li class="active"><a href="#tab_detail" data-toggle="tab" rel="tab_detail">API文档列表</a></li>
                        <li><a href="javascript:void(0)" onclick="addWiki()">添加文档</a></li>
                        <li><a href="./Home/Wiki/index" rel="{:U('Home/Wiki/index')}" target="_blank">文档中心</a></li>

                        <li class="pull-right"><a href="javascript:void(0)" onclick="refresh()" class="text-muted" title="刷新"><i class="fa fa-refresh"></i></a></li>
                    </ul>

                    <div class="tab-content">

                        <div class="tab-pane active" id="tab_detail">


                            <div style="width:100%;">

                                    <div class="box-body no-padding">

                                        <div class="col-md-3">
                                            <div class="box box-warning">
                                                <div class="box-header with-border">
                                                    <h3 class="box-title">文档列表</h3>
                                                    <div class="box-tools pull-right">
                                                        <button data-widget="collapse" class="btn btn-box-tool"><i class="fa fa-minus"></i></button>
                                                    </div><!-- /.box-tools -->
                                                </div><!-- /.box-header -->
                                                <div class="box-body" style="display: block;">

                                                    <script type="text/javascript">
                                                        //打开新窗口
                                                        function openwinx(url,name,w,h) {
                                                            window.open(url);
                                                        }
                                                        //配置
                                                        var setting = {
                                                            view: {
                                                                fontCss: function(treeId, treeNode) {
                                                                    return treeNode.status == 0 ? {color:"#aaaaaa"} : {};
                                                                },
                                                                addHoverDom: function(treeId, treeNode) {
                                                                    if(treeNode.aid==0) {
                                                                        $('#'+treeNode.tId+'_edit').remove();
                                                                        $('#'+treeNode.tId+'_remove').remove();
                                                                    }
                                                                    var sObj = $("#" + treeNode.tId + "_span");
                                                                    if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
                                                                    var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='添加' onfocus='this.blur();'></span>";
                                                                    sObj.after(addStr);
                                                                    var btn = $("#addBtn_"+treeNode.tId);
                                                                    if (btn) btn.bind("click", function(){
                                                                        //var html = '确定添加子文档?';
                                                                        var html = '<div><span class="text-blue">确定添加子文档?</span><br><br><div class="form-group"><label for="">文档名称</label><input type="text" placeholder="文档名称" value="新建文档" name="title" id="title" class="form-control" style=""></div><br></div>';
                                                                        BootstrapDialog.confirm(html, function(result){
                                                                            if(result) {
                                                                                var url = "{:U('addnew')}";
                                                                                var params = {
                                                                                    pid : treeNode.aid,
                                                                                    title : $('.modal-body').find('#title').val(),
                                                                                };
                                                                                ajaxGet(url, params, 0.5, function (result) {
                                                                                    if(result.url){
                                                                                        location.href = result.url;
                                                                                    }else{
                                                                                        location.reload();
                                                                                    }
                                                                                    /*
                                                                                    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                                                                                    var aid = result.aid ? result.aid : 0;
                                                                                    var title = result.title ? result.title : '新建文档';
                                                                                    treeObj.addNodes(treeNode, {aid:aid, pId: treeNode.aid, title:title});
                                                                                    */
                                                                                }, function() {
                                                                                    MsgBox.error('操作失败');
                                                                                });
                                                                            }
                                                                        });
                                                                        return false;
                                                                    });
                                                                },
                                                                removeHoverDom: function(treeId, treeNode) {
                                                                    $("#addBtn_"+treeNode.tId).unbind().remove();
                                                                },
                                                                //selectedMulti: false
                                                            },
                                                            edit: {
                                                                enable: true,
                                                                editNameSelectAll: false,
                                                                showRemoveBtn: true,
                                                                showRenameBtn: true,
                                                                renameTitle: "重命名",
                                                                removeTitle: "删除"
                                                            },
                                                            data: {
                                                                key: {
                                                                    name: "title"
                                                                },
                                                                simpleData: {
                                                                    enable: true,
                                                                    idKey: "aid",
                                                                    pIdKey: "pid",
                                                                    rootPId: 0,
                                                                }
                                                            },
                                                            callback: {
                                                                beforeRename: function(treeId, treeNode, newName, isCancel) {
                                                                    return newName.length > 0;
                                                                },
                                                                beforeRemove: function(treeId, treeNode) {
                                                                    BootstrapDialog.confirm('确定删除此文档吗?', function(result){
                                                                        if(result) {
                                                                            var url = "{:U('del')}";
                                                                            var params = {
                                                                                aid : treeNode.aid,
                                                                            };
                                                                            ajaxGet(url, params, 1, function (result) {
                                                                                if(result.url){
                                                                                    location.href = result.url;
                                                                                }else{
                                                                                    //location.reload();
                                                                                }
                                                                                var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                                                                                treeObj.removeNode(treeNode);
                                                                            }, function() {
                                                                                MsgBox.error('操作失败');
                                                                            });
                                                                        }
                                                                    });
                                                                    return false;
                                                                },
                                                                beforeClick: function (treeId, treeNode) {
                                                                    if (treeNode.isParent) {
                                                                        //zTree.expandNode(treeNode);
                                                                        //return false;
                                                                    } else {
                                                                        return true;
                                                                    }
                                                                    return true;
                                                                },
                                                                onClick:function(event, treeId, treeNode){
                                                                    //栏目ID
                                                                    var aid = treeNode.aid;
                                                                    //保存当前点击的栏目ID
                                                                    setCookie('tree_aid',aid);
                                                                    if(treeNode.loadurl) {
                                                                        editWiki(treeNode.loadurl);
                                                                    }
                                                                },
                                                                //重命名
                                                                onRename:function(event, treeId, treeNode, isCancel) {
                                                                    var url = "{:U('rename')}";
                                                                    var params = {
                                                                        aid : treeNode.aid,
                                                                        title : treeNode.title
                                                                    };
                                                                    ajaxGet(url, params, 1, function (result) {
                                                                        if(result.url){
                                                                            location.href = result.url;
                                                                        }else{
                                                                            //location.reload();
                                                                        }
                                                                        var aid = $('#aid', $('#dataArea')).val();
                                                                        if(aid==treeNode.aid) {
                                                                            $('#title', $('#dataArea')).val(treeNode.title);
                                                                        }
                                                                    }, function() {
                                                                        MsgBox.error('操作失败');
                                                                    });
                                                                },
                                                                //移动
                                                                onDrop:function(event, treeId, treeNodes, targetNode, moveType) {
                                                                    if(targetNode) {
                                                                        var ids = [];
                                                                        for(var i=1; i<=treeNodes.length; i++) {
                                                                            ids.push(treeNodes[i-1].aid)
                                                                        }
                                                                        var url = "{:U('move')}";
                                                                        var params = {
                                                                            aid : ids.join(','),
                                                                            pid : targetNode.aid,
                                                                        };
                                                                        ajaxGet(url, params, 1, function (result) {
                                                                            if(result.url){
                                                                                location.href = result.url;
                                                                            }else{
                                                                                location.reload();
                                                                            }
                                                                        }, function() {
                                                                            MsgBox.error('操作失败');
                                                                        });
                                                                    }
                                                                }
                                                            }
                                                        };
                                                        function addHoverDom(treeId, treeNode) {
                                                            var sObj = $("#" + treeNode.tId + "_span");
                                                            if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
                                                            var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
                                                                    + "' title='add node' onfocus='this.blur();'></span>";
                                                            sObj.after(addStr);
                                                            var btn = $("#addBtn_"+treeNode.tId);
                                                            if (btn) btn.bind("click", function(){
                                                                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                                                                zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
                                                                return false;
                                                            });
                                                        };
                                                        //节点数据
                                                        var zNodes ={$json|json_encode};
                                                        //zTree对象
                                                        var zTree = null;
                                                        $(function(){
                                                            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                                                            zTree = $.fn.zTree.getZTreeObj("treeDemo");
                                                            $("#ztree_expandAll").click(function(){
                                                                if($(this).data("open")){
                                                                    zTree.expandAll(false);
                                                                    $(this).data("open",false);
                                                                }else{
                                                                    zTree.expandAll(true);
                                                                    $(this).data("open",true);
                                                                }
                                                            });
                                                            //定位到上次打开的栏目，进行展开tree_aid
                                                            var tree_aid = getCookie('tree_aid');
                                                            if(tree_aid){
                                                                var nodes = zTree.getNodesByParam("aid", tree_aid, null);
                                                                zTree.selectNode(nodes[0]);
                                                                editWiki(nodes[0].loadurl);
                                                            }
                                                        });
                                                    </script>
                                                    <div>
                                                        <ul class="ztree" style="padding:0px;">
                                                            <li> <a title="全部展开、折叠 "><span class="button ico_open" style="background:url(http://shuipf.com/statics/images/application_side_expand.png) 0 0 no-repeat;"></span><span id="ztree_expandAll" data-open="false">全部展开、折叠 </span></a> </li>
                                                        </ul>
                                                        <ul id="treeDemo" class="ztree">
                                                        </ul>
                                                    </div>


                                                </div><!-- /.box-body -->
                                            </div><!-- /.box -->
                                        </div>

                                        <div class="col-md-9">
                                            <div class="box box-default box-solid">
                                                <div class="box-header with-border">
                                                    <h3 class="box-title">文档编辑区</h3>
                                                    <div class="box-tools pull-right">
                                                        <button data-widget="collapse" class="btn btn-box-tool"><i class="fa fa-minus"></i></button>
                                                    </div><!-- /.box-tools -->
                                                </div><!-- /.box-header -->
                                                <div class="box-body" style="display: block;">
                                                    <div id="dataArea"></div>
                                                </div><!-- /.box-body -->
                                            </div><!-- /.box -->
                                        </div>


                                    </div><!-- /.box-body -->

                            </div>
                        </div><!-- /.tab-pane -->

                    </div><!-- /.tab-content -->


                </div><!-- nav-tabs-custom -->
            </div><!-- /.col -->

        </div> <!-- /.row -->
        <!-- END CUSTOM TABS -->

    </section><!-- /.content -->
</div><!-- /.content-wrapper -->

<script>
    //添加文档
    function addWiki(id) {
        id = isNaN(id) ? '' : id;
        var title = '添加文档';
        var url = "{:U('add',array('pid'=>''))}" + id;
        ajaxForm(url, title);
    }

    //编辑wiki文档
    function editWiki(url) {
        $('#dataArea').load(url);
    }

    //刷新
    function refresh() {
        location.reload();
    }
</script>

<include file='Common:footer'/>